<template>
  <div class="total-box">
    <div class="app-container">
      <HkSearchform
        :data="queryParams"
        :field-list="filterInfo.fieldList"
        :list-type-info="dictList"
        @handleSearch="handleQuery"
        @handleReset="resetQuery"
        @handleEvent="handleEvent"
      />

      <el-row :gutter="10">
        <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新建</el-button>
        <el-button
          icon="el-icon-delete"
          size="small"
          :disabled="!ids.length"
          @click="handleDelete"
        >删除</el-button>
        <el-button icon="el-icon-download" size="small" @click="importTemplate">下载模板</el-button>
        <el-button size="small" @click="handleImport">导入</el-button>
        <el-button size="small" @click="handleExport">导出</el-button>
      </el-row>

      <hk-table
        ref="tableRef"
        :loading="loading"
        :table-data="tableData"
        :table-columns="tableList"
        :selection="true"
        :show-index="true"
        :total="total"
        :operate="true"
        :table-height="80"
        :query-params="queryParams"
        @handleSelectionChange="handleSelectionChange"
        @changePage="getDatalist"
      >
        <template #innerIpRange="scope">
          <el-popover placement="bottom-start" width="200" trigger="hover">
            <div class="popver">
              <p>开始IP：{{ scope.row.inner.startIp ||'' }}</p>
              <p>结束IP：{{ scope.row.inner.endIp ||'' }}</p>
              <p>掩码：{{ scope.row.inner.mask ||'' }}</p>
              <p>容量：{{ scope.row.inner.count ||'' }}</p>
            </div>
            <span slot="reference">{{ scope.row.innerIpRange }}</span>
          </el-popover>
        </template>
        <template #outerIpRange="scope">
          <el-popover placement="bottom-start" width="200" trigger="hover">
            <div class="popver">
              <p>开始IP：{{ scope.row.outer.startIp||'' }}</p>
              <p>结束IP：{{ scope.row.outer.endIp||'' }}</p>
              <p>掩码：{{ scope.row.outer.mask ||'' }}</p>
              <p>容量：{{ scope.row.outer.count||'' }}</p>
            </div>
            <span slot="reference">{{ scope.row.outerIpRange }}</span>
          </el-popover>
        </template>
        <template #operate="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </hk-table>
    </div>
    <!-- 新增和编辑的对话框 -->
    <edit-dialog
      ref="editDialogRef"
      :title="title"
      :keyword="queryParams.keyword"
      @handleSubmit="getDatalist"
    />
    <!-- 输出 -->
    <import-dialog ref="import_dia" @submit="getDatalist" />
  </div>
</template>

<script>
import HkTable from '@/components/Hk-Table/index.vue'
import EditDialog from './component/emitDialog.vue'
import HkSearchform from '@/components/HkSearchform/index.vue'
import DictLabel from '@/components/Common/dictLabel.vue'
import ImportDialog from './component/ImportDialog.vue'
import { pageList, deleteList } from '@/api/approval/applicationAccess.js'

export default {
  components: {
    HkTable,
    EditDialog,
    HkSearchform,
    DictLabel,
    ImportDialog
  },
  data() {
    return {
      treeId: '',
      // 搜索项配置
      filterInfo: {
        fieldList: [{ label: '关键字', type: 'input', field: 'keyword' }]
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,

      // 表格数据
      tableData: [],
      //  表格
      tableList: [
        { label: '应用系统名称', param: 'appName' },
        { label: '部署服务器及主服务', param: 'serverIp' },
        { label: '对外服务端口', param: 'port' },
        { label: '内部授权访问IP段', param: 'innerIpRange', type: 'slot' },
        { label: '外部授权访问IP段', param: 'outerIpRange', type: 'slot' },
        { label: '审批时间', param: 'approvalTime', sortable: true }
      ],
      // 弹出层标题
      title: '',

      // 查询参数
      queryParams: {
        size: 10,
        current: 1
      },
      // 列表规则
      dictList: {
        foList: [
          {
            label: '是',
            value: '0'
          },
          {
            label: '否',
            value: '1'
          }
        ],

        secLevelList: [],
        domainList: []
      }
    }
  },

  created() {
    this.getDatalist()
  },
  methods: {
    /** 查询用户列表 */
    getDatalist(val) {
      this.loading = true
      this.queryParams = { ...this.queryParams, ...val, scrollToTop: true }

      pageList(this.queryParams).then(res => {
        this.total = res.total
        this.tableData = res.list
        this.loading = false
        this.queryParams.scrollToTop = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery(val) {
      this.queryParams.current = 1
      this.getDatalist()
    },
    // 重置表单
    resetQuery() {
      for (const key in this.queryParams) {
        this.queryParams[key] = undefined
      }
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getDatalist()
    },
    // 搜索框变化
    handleEvent(val) {
      const field = val.field
      const value = val.value
      this.queryParams = {
        ...this.queryParams,
        ...{
          [field]: value
        }
      }
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.title = '添加'
      this.$refs.editDialogRef.show()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = '编辑'
      this.$refs.editDialogRef.show(row.id)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids.join(',')
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          return deleteList(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getDatalist()
        })
        .catch(error => {
          console.log(error)
        })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.import_dia.upload.open = true
    },
    /** 下载模板操作 */
    importTemplate() {
      this.gDownload(window.baseUrl.icp + '/approve/app/template', {}, `应用访问审批模板.xlsx`)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.gDownload(
        window.baseUrl.icp + '/approve/app/export',
        {
          ...this.queryParams
        },
        `应用访问审批${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
  <style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
}
.total-box {
  font-size: 14px;
  display: flex;
  width: 100%;
  height: 100%;
}
.head-container {
  overflow: auto;
  padding: 20px;
  height: 100%;
  width: 270px;
  border-right: 1px solid rgba(0, 0, 0, 0.08);
}
.targetTable {
  flex: 1;
  overflow: hidden;
  padding-bottom: 0;
}
.el-row {
  display: flex;
  flex-wrap: wrap;
  margin: 0 !important;
}
.popver{
  p+p{
    margin-top: 5px;
  }
}
::v-deep .el-table{
  overflow: hidden;
}
</style>

